package course.d1218;
import java.util.*;

/*
public interface Comparable<T>
此接口强行对实现它的每个类的对象进行整体排序
这种排序被称为类的自然排序，类的compareTo方法被称为它的自然比较方法

int compareTo(T t);
比较该对象与指定对象的排序
如果该对象小于、等于或大于指定对象，则分别返回负整数、零或正整数
 */
public class p3 {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(111, "aaa"));
        list.add(new Student(333, "bbb"));
        list.add(new Student(-111, "ccc"));
        Collections.sort(list);
        System.out.println(list);
        List<Employee> list1 = new ArrayList<>();
        list1.add(new Employee(12,1000));
        list1.add(new Employee(12,5000));
        list1.add(new Employee(13,2000));
        Collections.sort(list1);
        System.out.println(list1);
    }
}

class Student implements Comparable<Student> {
    private Integer id;
    private String name;

    public Student(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Student student) {
        return this.id.compareTo(student.id);
    }
}

class Employee implements Comparable<Employee> {
    private Integer age;
    private Integer wage;

    public Employee(Integer age, Integer wage) {
        this.age = age;
        this.wage = wage;
    }

    public Employee() {
    }

    @Override
    public String toString() {
        return "Employee{" +
                "age='" + age + '\'' +
                ", wage='" + wage + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(age, employee.age) &&
                Objects.equals(wage, employee.wage);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, wage);
    }

    @Override
    public int compareTo(Employee employee) {
        if (this.wage>employee.wage){
            return -1;
        }else if (this.wage<employee.wage){
            return 1;
        }else {
            if (this.age>employee.age){
                return -1;
            }else if (this.age<employee.age){
                return 1;
            }else {
                return 0;
            }
        }

    }
}
